package org.tedu.day07springmvc.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.tedu.day07springmvc.mapper.UserMapper;
import org.tedu.day07springmvc.pojo.dto.UserDTO;
import org.tedu.day07springmvc.pojo.entity.User;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
public class UserController {
    @Autowired
    private UserMapper userMapper;
//    @RequestMapping("/regUser")
//    @ResponseBody
//    public String regUser(HttpServletRequest request) {
//        String username = request.getParameter("username");
//        String password = request.getParameter("password");
//        String nickname = request.getParameter("nickname");
//        int age = Integer.valueOf(request.getParameter("age"));
//        System.out.println("username: " + username);
//        System.out.println("password: " + password);
//        System.out.println("nickname: " + nickname);
//        System.out.println("age: " + age);
//        return "注册成功";
//    }


//    @RequestMapping("/regUser")
//    @ResponseBody
//    public String regUser(String username, String password, String nickname, int age) {
//        System.out.println("username: " + username);
//        System.out.println("password: " + password);
//        System.out.println("nickname: " + nickname);
//        System.out.println("age: " + age);
//        return "注册成功";
//    }


    @RequestMapping("/regUser")
    @ResponseBody
    public String regUser(UserDTO userDTO) {
        System.out.println(userDTO);
        User user = new User();
        user.setUsername(userDTO.getUsername());
        user.setPassword(userDTO.getPassword());
        user.setNickname(userDTO.getNickname());
        user.setAge(userDTO.getAge());
        user.setCreated(new Date());
        int num = userMapper.insertUser(user);
        return num > 0 ? "插入成功" : "插入失败";
    }
}
